package com.ruoyi.factorydata.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.factorydata.domain.dto.ProcessDataDTO;
import com.ruoyi.factorydata.domain.vo.ProcessDataVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factorydata.domain.ProcessData;
import com.ruoyi.factorydata.service.IProcessDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工序信息Controller
 *
 * @author ruoyi
 * @date 2025-07-12
 */
@RestController
@RequestMapping("/processdata/processdata")
public class ProcessDataController extends BaseController {
    @Autowired
    private IProcessDataService processDataService;

    /**
     * 查询工序信息列表
     */
    @PreAuthorize("@ss.hasPermi('processdata:processdata:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProcessData processData) {
        startPage();
        if (processData != null && processData.getCategoryId() != null) {
            processData.setCategoryId(processData.getCategoryId() == 0 ? null : processData.getCategoryId());
        }
        List<ProcessDataVo> list = processDataService.selectProcessDataList(processData);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return tableDataInfo;
    }

    /**
     * 导出工序信息列表
     */
    @PreAuthorize("@ss.hasPermi('processdata:processdata:export')")
    @Log(title = "工序信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProcessData processData) {
        List<ProcessDataVo> list = processDataService.selectProcessDataList(processData);
        ExcelUtil<ProcessDataVo> util = new ExcelUtil<ProcessDataVo>(ProcessDataVo.class);
        util.exportExcel(response, list, "工序信息数据");
    }

    /**
     * 获取工序信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('processdata:processdata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(processDataService.selectProcessDataById(id));
    }

    /**
     * 新增工序信息
     */
    @PreAuthorize("@ss.hasPermi('processdata:processdata:add')")
    @Log(title = "工序信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProcessDataDTO processDataDTO) {
        boolean exists = processDataService.lambdaQuery()
                .eq(ProcessData::getProcessCode, processDataDTO.getProcessCode()).count() > 0;
        if (exists) {
            return warn("工序编号已存在");
        }
        processDataDTO.setCreateBy(getUserId().toString());
        return toAjax(processDataService.insertProcessData(processDataDTO));
    }

    /**
     * 修改工序信息
     */
    @PreAuthorize("@ss.hasPermi('processdata:processdata:edit')")
    @Log(title = "工序信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProcessDataDTO processDataDTO) {
        boolean exists = processDataService.lambdaQuery()
                .ne(ProcessData::getId, processDataDTO.getId())
                .eq(ProcessData::getProcessCode, processDataDTO.getProcessCode())
                .count() > 0;
        if (exists) {
            return warn("工序编号已存在");
        }
        return toAjax(processDataService.updateProcessData(processDataDTO));
    }

    /**
     * 删除工序信息
     */
    @PreAuthorize("@ss.hasPermi('processdata:processdata:remove')")
    @Log(title = "工序信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(processDataService.deleteProcessDataByIds(ids));
    }
}
